//
//  UserManager.m
//  ITMS
//
//  Created by 中矿科技有限公司 on 16/7/13.
//  Copyright © 2016年 中矿科技有限公司. All rights reserved.
//

#import "UserManager.h"
#define USERDEFAULT_USERINFO    @"userdefault_user_info"

@implementation UserManager
static id _userManager;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _userManager = [super allocWithZone:zone];
    });
    return _userManager;
}
//获取单列
+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _userManager = [[self alloc] init];
    });
    return _userManager;
}



- (id)copyWithZone:(NSZone *)zone
{
    return _userManager;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
    return _userManager;
}

//登录
- (BOOL)login:(NSDictionary*)userInfo {
    return  [self storeUserInfoToUserDefaults:userInfo];
}
//退出登录
- (void)logout {
    if ([self isLogin]) {
      //  [[NSUserDefaults standardUserDefaults] removeObjectForKey:USERDEFAULT_USERINFO];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}
//是否登录
- (BOOL)isLogin {
    BOOL result = NO;
    if ([[NSUserDefaults standardUserDefaults] objectForKey:USERDEFAULT_USERINFO]) {
        result = YES;
    }
    return result;
}

//检查并保存用户数据
- (BOOL)storeUserInfoToUserDefaults:(NSDictionary *)userInfo{
    if (![userInfo[@"name"] isKindOfClass:[NSString class]]) {
        [userInfo setValue:@"" forKey:@"name"];

    }if (![userInfo[@"userName"] isKindOfClass:[NSString class]]) {
        [userInfo setValue:@"" forKey:@"userName"];
    }
    if (![userInfo[@"emailAddress"] isKindOfClass:[NSString class]]) {
        [userInfo setValue:@"" forKey:@"emailAddress"];
    }
    if (![userInfo[@"phoneNumber"] isKindOfClass:[NSString class]]) {
        [userInfo setValue:@"" forKey:@"phoneNumber"];
    }
    if (![userInfo[@"positionTitle"] isKindOfClass:[NSString class]]) {
        [userInfo setValue:@"" forKey:@"positionTitle"];
    }
    if (![userInfo[@"ouName"] isKindOfClass:[NSString class]]) {
        [userInfo setValue:@"" forKey:@"ouName"];
    }
    if (![userInfo[@"companyName"] isKindOfClass:[NSString class]]) {
        [userInfo setValue:@"" forKey:@"companyName"];
    }
//    if (![userInfo[@"id"] isKindOfClass:[NSNumber class]]) {
//        [userInfo setValue:@"" forKey:@"id"];
//    }
    [[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:USERDEFAULT_USERINFO];
    BOOL isSuccess = [[NSUserDefaults standardUserDefaults] synchronize];
    if (isSuccess) {
       // NSLog(@"保存用户数据成功");
    }else{
       // NSLog(@"保存用户数据失败");
    }
    return isSuccess;
}

- (NSString*)getUserId{
    return [[self getUserInfoObj] objectForKey:@"id"];
}

//获取昵称
- (NSString*)getNickname {
    return [[self getUserInfoObj] objectForKey:@"name"];
}

/**
 获取用户姓名
 */
- (NSString*)getUserName{
    return [[self getUserInfoObj] objectForKey:@"userName"];
}

/**
 获取邮箱信息

 @return 邮箱
 */
- (NSString *)getEmailAddress{
    return [[self getUserInfoObj] objectForKey:@"emailAddress"];
}

/**
 获取联系电话

 @return 联系电话
 */
- (NSString*)getMobile {
    return [[self getUserInfoObj] objectForKey:@"phoneNumber"];
}

- (NSString*)getCompanyName{
    return [[self getUserInfoObj] objectForKey:@"companyName"];
}

- (NSString*)getOuName{
    return [[self getUserInfoObj] objectForKey:@"ouName"];
}

- (NSString*)getPositionTitle{
    return [[self getUserInfoObj] objectForKey:@"positionTitle"];
}



//获取用户对象
- (NSDictionary *)getUserInfoObj {
    return [[NSUserDefaults standardUserDefaults] objectForKey:USERDEFAULT_USERINFO];
}
@end
